@extends('layouts.layout')

@section('header')

    <title>提交订单</title>

    <link rel="stylesheet" type="text/css" href="{{URL::asset('static/css/confirm_order.css')}}"/>

@endsection

@section('content')
<div class="main">

        <a class="address haveAddress shadow" href="/user/address">
            <div class="address_icon">
                <img src="{{URL::asset('static/img/address.png')}}" >
            </div>
            <div class="user_info">
                <p><i></i><span></span></p>
                <div></div>
            </div>
            <div class="" style="margin-left: .2rem;">
                <img src="{{URL::asset('static/img/right.png')}}" style="height: .28rem;width: auto;">
            </div>
        </a>

        @if (!empty($address))
            <a class="address phpAddress shadow" href="/user/address">
                <div class="address_icon">
                    <img src="{{URL::asset('static/img/address.png')}}" >
                </div>
                <input type="hidden" class="defaultAddress" value="{{ $address->id }}">

                <div class="user_info">
                    <p><i>{{ $address->name }}</i><span>{{ $address->mobile }}</span></p>
                    <div>{{ $address->area }}{{ $address->address }}</div>
                </div>

                <div class="" style="margin-left: .2rem;">
                    <img src="{{URL::asset('static/img/right.png')}}" style="height: .28rem;width: auto;">
                </div>
            </a>
        @else
            <a class="noAddress shadow" href="/user/address">
                <p>+</p>
                <span>添加收货地址</span>
            </a>
        @endif

			<ul class="list shadow" style="margin-top: .3rem;">
				<li>
					<a href="javascript:;" class="clearfix">
						<div class="pull-left"><img src="" ></div>
						<div class="pull-left">
							<p class="ellipsis name">面膜描摹</p>
							<span class="price"></span>
						</div>
						<div class="addBox">
							<span class="reduce">-</span>
							<i>1</i>盒
							<span class="add">+</span>
						</div>
					</a>
				</li>
			</ul>
		</div>

		<div class="footer">
			<div>
				合计：<span class="totalPrice">￥<i></i></span>
			</div>
			<div>
				<p class="btn">提交订单</p>
			</div>
		</div>

@endsection

@section('script')
    <script type="text/javascript">
        $(function(){
            var address = sessionStorage.getItem('address')
            var ajaxData = ""; //添加或修改后的ajax数据
            var isUse = 1;  //是否使用积分

            if(Boolean(address)){ // 如果本地有地址
                $(".noAddress").hide()
                // $(".phpAddress").css('display','block')
                $(".phpAddress").css('display','none')
                $(".haveAddress").show()

                var address = JSON.parse(address)
                $(".haveAddress .user_info i").html(address.name)
                $(".haveAddress .user_info span").html(address.mobile)
                $(".haveAddress .user_info div").html(address.address)

            }else{   // 本地没有选择地址
                $(".noAddress").show()
                $(".address").hide()

                   // 有默认地址
                   if($('.defaultAddress').val()){
                       $(".phpAddress").show()
                   }
            }

            // 提交订单
            $(".footer .btn").click(function(){

                if(!Boolean(address) && !Boolean($('.defaultAddress').val())){
                    layer.open({
                        content: '请选择收货地址!'
                        ,skin: 'msg'
                        ,time: 1
                      });
                    return false
                }else{

                    var data = {}
                    data['goods_id'] = info.id;
                    data['num'] = $('.addBox i').text();

                    // 选择了地址
                    if(Boolean(address)){
                        data['address_id'] = address.address_id;
                        tjAjax(data)
                    }else{ // 使用默认地址
                        data['address_id'] = $(".defaultAddress").val()
                        tjAjax(data)
                    }
                }

            })

            function tjAjax(data){
                commonAjax({type:"post",url:'/order/subOrder',data},function(msg){
                    var msg1 = msg
                    WeixinJSBridge.invoke(
                    'getBrandWCPayRequest',{
                        "appId":msg1.appId,
                        "timeStamp":msg1.timeStamp,
                        "nonceStr":msg1.nonceStr,
                        "package":msg1.package,
                        "signType":"MD5",
                        "paySign":msg1.paySign
                    },
                    function(res){
                        layer.closeAll()
                        // 使用以上方式判断前端返回,微信团队郑重提示：
                        //res.err_msg将在用户支付成功后返回ok，但并不保证它绝对可靠。
                        history.replaceState(null, null, "/user/my")
                        sessionStorage.removeItem("address")
                        location.href = "/my/order"
                    })
                })
            }
            // 获取商品信息
            var info = JSON.parse(sessionStorage.getItem('goodsInfo'))

            $(".list img").attr('src','/uploads/'+info.goods_cover)
            $(".list .name").html(info.goods_name)
            $(".list .price").html("￥"+info.goods_prices)
            $(".list .addBox i").html(info.num)
            $('.totalPrice i').html(info.goods_prices*info.num)

            // 减
            var reduceStatus = true;
            $(".reduce").click(function(e){
                if(!reduceStatus){
                    return false;
                }
                reduceStatus = false
                if($('.addBox i').text() == 1){
                    return false;
                }
                $('.addBox i').text($('.addBox i').text()-1)
                $('.totalPrice i').html((info.goods_prices*$('.addBox i').text()).toFixed(2))
                reduceStatus = true
                e.stopPropagation();
                return false;
            })
            // 加
            var addStatus = true;
            $(".add").click(function(e){
                if(!addStatus){
                    return false;
                }
                addStatus = false;
                $('.addBox i').text(Number($('.addBox i').text())+1);
                $('.totalPrice i').html((info.goods_prices*$('.addBox i').text()).toFixed(2))
                addStatus = true;
                e.stopPropagation();
                return false;
            })


            window.onpageshow = function(event) {
                if (event.persisted || window.performance &&
                    window.performance.navigation.type == 2) {
                    window.location.reload();
                }
            }
        })
    </script>

@endsection
